package com.nt.train.task;

import com.nt.train.entity.GroupQuery;
import com.nt.train.entity.GroupUser;
import com.nt.train.entity.SendEmailTask;
import com.nt.train.enums.GroupType;
import com.nt.train.service.context.GroupSelectFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hlf
 * @version 1.0
 * @date 2022/8/5 14:30
 */
@Slf4j
//@EnableScheduling
@Component
public class GroupUserTask {

    @Autowired
    private GroupSelectFactory groupSelectFactory;

    /**
     * 模拟定时发送营销邮件
     */
    @Scheduled(cron = "0/10 * * * * ?")
    public void sendEmailTask() {
        System.out.println("模拟定时任务执行");
        List<SendEmailTask> taskList = new ArrayList<>();
        for (GroupType groupType : GroupType.values()) {
            System.out.println("筛选方式："+groupType.name());
            GroupQuery groupQuery = new GroupQuery("虚头巴脑的 " + groupType.name() + " 查询条件");
            taskList.add(SendEmailTask.of(groupType, groupQuery));
        }

        taskList.forEach(task -> {
            List<GroupUser> groupUsers = groupSelectFactory.getGroupSelect(task.getType()).queryUser(task.getQuery());
            log.info("groupUsers = {}", groupUsers);
        });
    }

//    @Data
//    @NoArgsConstructor
//    @AllArgsConstructor(staticName = "of")
//    static class SendEmailTask implements Serializable {
//        private static final long serialVersionUID = -3461263089669779193L;
//        private GroupType type;
//        private GroupQuery query;
//    }
}
